<template>
  <div>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="form.region" placeholder="选择注册角色">
          <el-option label="管理员" value="管理员"></el-option>
          <el-option label="教师" value="教师"></el-option>
          <el-option label="学生" value="学生"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-input v-model="form.sex"></el-input>
      </el-form-item>
      <el-form-item label="家庭住址">
        <el-input type="textarea" v-model="form.address"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">确认</el-button>
        <el-button type="info" @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Register",
  data() {
    return {
      form: {
        username: "",
        password: "",
        region: "",
        name: "",
        sex: "",
        address: "",
      },
    };
  },
  methods: {
    async onSubmit() {
      console.log(this.form);
      let result = await this.$http.inserUser(
        this.form.username,
        this.form.password,
        this.form.region,
        this.form.name,
        this.form.sex,
        this.form.address
      );
      if(result.code==20000)
      {
        console.log(result);
        alert("注册成功！");
        this.$router.push('/');
      }
      else{
        alert("注册失败！");
        this.$router.push('/Register');
      }
      
    },
    cancel(){
        this.$router.push('/');
    }
  },
};
</script>
